package com.copa.provider.games;

import com.copa.entity.Games;
import com.copa.service.GamesService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author copa
 * @createDate 2022-06-22 22:12
 * @function
 */
@Component
public class GameTimeInfo {

    @Resource
    private GamesService gamesService;

    public void getGamesInfo() {
        try {
            Document document = Jsoup.connect("https://www.vgtime.com/game/release.jhtml")
                    .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36")
                    .get();
            Elements oneGameTimesEle = document.getElementsByClass("vg_ss_box");
            for (Element oneDay : oneGameTimesEle) {
                String time = oneDay.getElementsByClass("ss_tit").text();
                Elements gameInfo = oneDay.getElementsByTag("dd");
                for (Element oneGame : gameInfo) {
                    String imgUrl = oneGame.getElementsByClass("img_box").get(0).getElementsByTag("img").attr("src");
                    Element nameEle = oneGame.getElementsByClass("info_box").get(0);
                    String chiName = nameEle.getElementsByTag("h2").text();
                    String otherName = nameEle.getElementsByTag("span").text();
                    Elements descEle = oneGame.getElementsByClass("game_descri");
                    List<String> platformList = new ArrayList<>();
                    List<String> gameGeneList = new ArrayList<>();
                    String platformStr = "";
                    String gameGeneStr = "";
                    for (Element desc : descEle) {
                        Elements platformEle = desc.getElementsByClass("platform_detail_box").get(0).getElementsByTag("span");
                        for (Element platform : platformEle) {
                            platformList.add(
                                    "true".equals(platform.attr("data-cn"))
                                            ? platform.text() + "(中)"
                                            : platform.text());
                        }
                        Elements gameGeneEle = desc.getElementsByClass("game_gene").get(0).getElementsByTag("span");
                        for (Element gameGene : gameGeneEle) {
                            gameGeneList.add(gameGene.text());
                        }
                        platformStr = String.join(",", platformList);
                        gameGeneStr = String.join(",", gameGeneList);
                    }
                    Games games = Games.builder()
                            .imgUrl(imgUrl)
                            .gameGene(gameGeneStr)
                            .platform(platformStr)
                            .saleTime(time)
                            .chiName(chiName)
                            .otherName(otherName)
                            .build();
                    gamesService.insert(games);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
